package com.nutricia.core.service.convertor;

import com.nutricia.common.dal.persistant.MenuAndPermission;
import com.nutricia.core.model.domain.MenuAndPermissionDO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 权限联合菜单转换器
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
public class MenuUnionPermissionServiceConvertor {


    /**
     * 将 MenuAndPermission 转化为 MenuAndPermissionDo
     *
     * 递归获取下级菜单数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午6:13
     */
    public static MenuAndPermissionDO convertToMenuAndPermissionDo(MenuAndPermission menuAndPermission){
        if(null == menuAndPermission){
            return null;
        }
        MenuAndPermissionDO menuAndPermissionDo = new MenuAndPermissionDO();
        menuAndPermissionDo.setMenuId(menuAndPermission.getMenuId());
        menuAndPermissionDo.setMenuName(menuAndPermission.getMenuName());
        menuAndPermissionDo.setSystemPermissionDOS(
                SystemPermissionServiceConvertor.convertToSystemPermissionDOs(menuAndPermission.getSystemPermissions()));
        menuAndPermissionDo.setSubMenus(MenuUnionPermissionServiceConvertor.convertToMenuAndPermissionDoS(menuAndPermission.getSubMenus()));
        return menuAndPermissionDo;
    }

    /**
     * 将 MenuAndPermissions 转化为 MenuAndPermissionDos
     *
     * @param menuAndPermissions
     * @return
     */
    public static List<MenuAndPermissionDO> convertToMenuAndPermissionDoS(List<MenuAndPermission> menuAndPermissions){
        if(CollectionUtils.isNotEmpty(menuAndPermissions)){
            List<MenuAndPermissionDO> menuAndPermissionDos = new ArrayList<>(menuAndPermissions.size());
            menuAndPermissions.forEach(menuAndPermission -> {
                menuAndPermissionDos.add(MenuUnionPermissionServiceConvertor.convertToMenuAndPermissionDo(menuAndPermission));
            });
            return menuAndPermissionDos;
        }
        return Collections.emptyList();
    }

}
